
package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zzyl.dto.RoomDto;
import com.zzyl.entity.Room;
import com.zzyl.mapper.RoomMapper;
import com.zzyl.service.BedService;
import com.zzyl.service.RoomService;
import com.zzyl.vo.BedVo;
import com.zzyl.vo.RoomVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private BedService bedService;


    /**
     * 增加房间
     * @param roomDto 房间 dto 对象
     * @return 受影响的行数
     */
//    @Override
//    public int addRoom(RoomDto roomDto) {
//        Room room = new Room();
//        BeanUtils.copyProperties(roomDto, room);
//        return roomMapper.insert(room);
//    }

    /**
     * 删除房间
     * @param id 房间 id
     * @return 受影响的行数
     */
//    @Override
//    public int deleteRoom(Long id) {
//        return roomMapper.deleteById(id);
//    }

    /**
     * 更新房间信息
     * @param roomDto 房间 dto 对象
     * @return 受影响的行数
     */
//    @Override
//    public int updateRoom(RoomDto roomDto) {
//        Room room = new Room();
//        BeanUtils.copyProperties(roomDto, room);
//        return roomMapper.updateById(room);
//    }
//
    /**
     * 根据 id 获取房间信息
     * @param id 房间 id
     * @return 房间 dto 对象
     */
//    @Override
//    public RoomVo getRoom(Long id) {
//        Room room = roomMapper.selectById(id);
//        RoomVo roomVo = new RoomVo();
//        // 将房间实体对象拷贝到房间 vo 对象
//        BeanUtils.copyProperties(room, roomVo);
//        List<BedVo> bedsByRoomId = bedService.getBedsByRoomId(id);
//        List<BedVo> bedVos = bedsByRoomId.stream().filter(v -> v.getBedStatus().equals(1)).collect(Collectors.toList());
//        roomVo.setBedVoList(bedVos);
//
//        int totalBeds = bedsByRoomId.size();
//        int occupiedBeds = 0;
//        for (BedVo bedVo : bedsByRoomId) {
//            if (ObjectUtil.isNotEmpty(bedVo) && bedVo.getBedStatus() == 1) {
//                occupiedBeds++;
//            }
//        }
//
//        if (occupiedBeds != 0 && totalBeds != 0) {
//            double occupancyRate = (double) occupiedBeds / totalBeds;
//            roomVo.setOccupancyRate(occupancyRate);
//        } else {
//            roomVo.setOccupancyRate(0d);
//        }
//
//        roomVo.setTotalBeds(totalBeds);
//        roomVo.setOccupiedBeds(occupiedBeds);
//        return roomVo;
//    }

    /**
     * 获取所有房间视图对象
     * @return 房间视图对象列表
     */
//    @Override
//    public List<RoomVo> getAllRoomVos() {
//        List<Room> rooms = roomMapper.selectAll();
//        return rooms.stream().map(room -> {
//            RoomVo roomVo = new RoomVo();
//            // 将房间实体对象拷贝到房间视图对象
//            BeanUtils.copyProperties(room, roomVo);
//
//            return roomVo;
//        }).collect(Collectors.toList());
//    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     * @param floorId 楼层 id
     * @return 房间视图对象列表
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
       return roomMapper.selectByFloorId(floorId);
    }

    @Override
    public Map<String, Integer> countRoomByTypeName(List<String> collect) {
        if (CollUtil.isEmpty(collect)) {
            return new HashMap<>();
        }
        return roomMapper.countRoomByTypeName(collect);
    }

    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    /**
     * 获取所有房间（床位房型）
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsCheckedByFloorId(Long floorId) {
        return roomMapper.selectRoomsCheckedByFloorId(floorId);
    }

    @Override
    public int addRoom(RoomDto roomDto) {
        Room addroom = BeanUtil.toBean(roomDto, Room.class);
        return roomMapper.addRoom(addroom);
    }

    @Override
    public int deleteRoom(Long id) {
        return  roomMapper.deleteRoo(id);
    }

    @Override
    public int updateroom(RoomDto roomDto) {
        Room room = BeanUtil.toBean(roomDto, Room.class);
        return roomMapper.updateroom(room);
    }

    @Override
    public RoomVo getroomByid(long id) {
        Room room = roomMapper.getroom(id);//根据id查询指定房间并返回
        RoomVo roomVo = BeanUtil.toBean(room, RoomVo.class);//转换成视图对象
        List<BedVo> bedsByRoomId = bedService.getBedsByRoomId(id);//调用bed业务层方法根据房间id查询房间的床位

        List<BedVo> bedVoList = bedsByRoomId.stream().filter(roomBed -> roomBed.getBedStatus().equals(1)).collect(Collectors.toList());
        //获取已经入住的的床位
        roomVo.setBedVoList(bedVoList);//把获取到的入住床位赋值给该房间

        int totalBeds = bedsByRoomId.size();//获取床位总数量
        int occupiedBeds = 0;//定义一个变量用来求取使用床位数量
        for (BedVo bedVo : bedsByRoomId) {//遍历所有床位
            //如果不为null且状态值为1变量自增
            if (ObjectUtils.isNotEmpty(bedVo) && bedVo.getBedStatus().equals(1)) {
                occupiedBeds++;
            }
        }
        //如果总床位数量不为0且使用床位数量也不为0
        //求取它们相除的数值为使用率并赋值给该房间
        if (totalBeds != 0 && occupiedBeds != 0) {
            double occupancyRate = (double) totalBeds / occupiedBeds;
            roomVo.setOccupancyRate(occupancyRate);
        }else {//如果都为0直接设置为0
            roomVo.setOccupancyRate(0d);
        }
        roomVo.setTotalBeds(totalBeds);//将床位总数量赋值给该房间
        roomVo.setOccupiedBeds(occupiedBeds);//将使用床位数量赋值给该房间
        return roomVo;//返回
    }

    @Override
    public List<RoomVo> selectAll() {
        List<Room> rooms = roomMapper.selectAll();
        List<RoomVo> roomVos = rooms.stream().map(room -> {
            RoomVo roomVo = BeanUtil.toBean(room, RoomVo.class);
            return roomVo;
        }).collect(Collectors.toList());
        return roomVos;
    }

}

